package com.amesit.web.action.privilege;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;

import com.amesit.bean.PageView;
import com.amesit.bean.privilege.Department;
import com.amesit.service.privilege.DepartmentService;
import com.amesit.web.formbean.privilege.DepartmentForm;

@Controller("/control/department/list")
public class DepartmentAction extends Action {
	@Resource(name="departmentServiceBean")
	private DepartmentService departmentService;

	@Override @Privilege(model="department", privilegeValue="view")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		DepartmentForm formbean = (DepartmentForm) form;
		StringBuffer jpql = new StringBuffer("o.visible=?1");
		List<Object> params = new ArrayList<Object>();
		params.add(true);
		PageView<Department> pageView = new PageView<Department>(12, formbean.getPage());
		int firstindex = (pageView.getCurrentpage()-1)* pageView.getMaxresult();
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		pageView.setQueryResult(departmentService.getScrollData(Department.class, firstindex, 
				pageView.getMaxresult(), jpql.toString(), params.toArray(), orderby));
		request.setAttribute("pageView", pageView);		
		return mapping.findForward("list");
	}
}
